{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from numpy.random import RandomState"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 定义神经网络的相关参数和变量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch_size = 8\n",
    "dataset_size = 128\n",
    "x = tf.placeholder(tf.float32, shape=(None, 2), name=\"x-input\")\n",
    "y_ = tf.placeholder(tf.float32, shape=(None, 1), name='y-input')\n",
    "w1= tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1), trainable=True)\n",
    "y = tf.nn.tanh(tf.matmul(x, w1))\n",
    "# y = tf.matmul(x, w1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 设置自定义的损失函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 定义损失函数使得预测少了的损失大，于是模型应该偏向多的方向预测。\n",
    "loss_less = 10\n",
    "loss_more = 1\n",
    "loss = tf.reduce_sum(tf.where(tf.greater(y, y_), (y - y_) * loss_more, (y_ - y) * loss_less))\n",
    "train_step = tf.train.AdamOptimizer(0.001).minimize(loss)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 生成模拟数据集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rdm = RandomState(1)\n",
    "X = rdm.rand(dataset_size,2)\n",
    "Y = [[x1+x2+(rdm.rand()/10.0-0.05)] for (x1, x2) in X]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 训练模型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After 0 training step(s), w1 is: \n",
      "[[-0.81031823]\n",
      " [ 1.4855988 ]] \n",
      "\n",
      "After 1000 training step(s), w1 is: \n",
      "[[-0.02263279]\n",
      " [ 2.19889712]] \n",
      "\n",
      "After 2000 training step(s), w1 is: \n",
      "[[ 0.5252797 ]\n",
      " [ 2.58517075]] \n",
      "\n",
      "After 3000 training step(s), w1 is: \n",
      "[[ 0.9152925 ]\n",
      " [ 2.81545806]] \n",
      "\n",
      "After 4000 training step(s), w1 is: \n",
      "[[ 1.14963746]\n",
      " [ 2.96198297]] \n",
      "\n",
      "Final w1 is: \n",
      " [[ 1.32683945]\n",
      " [ 3.08070755]]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session() as sess:\n",
    "    init_op = tf.global_variables_initializer()\n",
    "    sess.run(init_op)\n",
    "    STEPS = 5000\n",
    "    for i in range(STEPS):\n",
    "        start = (i*batch_size) % dataset_size\n",
    "        end = (i*batch_size) % dataset_size + batch_size\n",
    "        sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})\n",
    "        if i % 1000 == 0:\n",
    "            print(\"After %d training step(s), w1 is: \" % (i))\n",
    "            print(sess.run(w1), \"\\n\")\n",
    "    print(\"Final w1 is: \\n\", sess.run(w1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5. 重新定义损失函数，使得预测多了的损失大，于是模型应该偏向少的方向预测。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After 0 training step(s), w1 is: \n",
      "[[-0.81031823]\n",
      " [ 1.48359871]] \n",
      "\n",
      "After 1000 training step(s), w1 is: \n",
      "[[ 0.05703859]\n",
      " [ 1.34017658]] \n",
      "\n",
      "After 2000 training step(s), w1 is: \n",
      "[[ 0.75677413]\n",
      " [ 1.16156507]] \n",
      "\n",
      "After 3000 training step(s), w1 is: \n",
      "[[ 1.07430112]\n",
      " [ 1.07888007]] \n",
      "\n",
      "After 4000 training step(s), w1 is: \n",
      "[[ 1.073686  ]\n",
      " [ 1.07864499]] \n",
      "\n",
      "Final w1 is: \n",
      " [[ 1.07497787]\n",
      " [ 1.07789981]]\n"
     ]
    }
   ],
   "source": [
    "loss_less = 1\n",
    "loss_more = 10\n",
    "loss = tf.reduce_sum(tf.where(tf.greater(y, y_), (y - y_) * loss_more, (y_ - y) * loss_less))\n",
    "train_step = tf.train.AdamOptimizer(0.001).minimize(loss)\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    init_op = tf.global_variables_initializer()\n",
    "    sess.run(init_op)\n",
    "    STEPS = 5000\n",
    "    for i in range(STEPS):\n",
    "        start = (i*batch_size) % dataset_size\n",
    "        end = (i*batch_size) % dataset_size + batch_size\n",
    "        sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})\n",
    "        if i % 1000 == 0:\n",
    "            print(\"After %d training step(s), w1 is: \" % (i))\n",
    "            print(sess.run(w1), \"\\n\")\n",
    "    print(\"Final w1 is: \\n\", sess.run(w1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6. 定义损失函数为MSE。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After 0 training step(s), w1 is: \n",
      "[[-0.81031823]\n",
      " [ 1.4855988 ]] \n",
      "\n",
      "After 1000 training step(s), w1 is: \n",
      "[[-0.14825174]\n",
      " [ 2.08503127]] \n",
      "\n",
      "After 2000 training step(s), w1 is: \n",
      "[[ 0.24612428]\n",
      " [ 2.34614587]] \n",
      "\n",
      "After 3000 training step(s), w1 is: \n",
      "[[ 0.52946812]\n",
      " [ 2.46511173]] \n",
      "\n",
      "After 4000 training step(s), w1 is: \n",
      "[[ 0.75288743]\n",
      " [ 2.47879934]] \n",
      "\n",
      "Final w1 is: \n",
      " [[ 0.9403336 ]\n",
      " [ 2.38410592]]\n"
     ]
    }
   ],
   "source": [
    "loss = tf.losses.mean_squared_error(y, y_)\n",
    "train_step = tf.train.AdamOptimizer(0.001).minimize(loss)\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    init_op = tf.global_variables_initializer()\n",
    "    sess.run(init_op)\n",
    "    STEPS = 5000\n",
    "    for i in range(STEPS):\n",
    "        start = (i*batch_size) % dataset_size\n",
    "        end = (i*batch_size) % dataset_size + batch_size\n",
    "        sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})\n",
    "        if i % 1000 == 0:\n",
    "            print(\"After %d training step(s), w1 is: \" % (i))\n",
    "            print(sess.run(w1), \"\\n\")\n",
    "    print(\"Final w1 is: \\n\", sess.run(w1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
